<?php

namespace App\Http\Services;

use Illuminate\Support\Facades\DB;
use App\Model\Couple;
use App\Model\RsRelationshipModel;
use App\Model\CouplingRing;
use App\Model\Users;
use App\Model\Keepsake;
use App\Model\Prop;
use App\Model\Api\RsRelationshipTasks;
use App\Model\RsRelationshipIntimacyLog;


/**
 * 自定义游戏服务
 *
 */
class CoupleService
{
    protected $couple;
    protected $rsRelationshipModel;
    protected $couplingRing;
    protected $user;
    protected $keepsake;
    protected $prop;
    protected $relationshipIntimacyLog;
    protected $rsRelationshipTasks;
    public function __construct()
    {
        $this->couple = new Couple();
        $this->rsRelationshipModel = new RsRelationshipModel();
        $this->couplingRing = new CouplingRing();
        $this->user = new Users();
        $this->keepsake = new Keepsake();
        $this->prop = new Prop();
        $this->relationshipIntimacyLog = new RsRelationshipIntimacyLog();
        $this->rsRelationshipTasks = new RsRelationshipTasks();
    }

    public function getList ($page=1, $pageSize=20, $userId = 0) {
        $return = [];
        $offset = ($page - 1) * $pageSize;
        $rsRows = $this->rsRelationshipModel->getList($offset, $pageSize, 3, $userId);
        $total = $this->rsRelationshipModel->getCount(3, $userId);
        $rsCodes = array();
        $userIds = array();
        $friendIds = array();
        foreach ($rsRows as $v) {
            $vArr = explode('_', $v['rsCode']);
            $vArr[] = $v['rsCode'];
            $userIds[] = $vArr[2];
            $friendIds[] = $vArr[3];
            $rsCodes[] = $vArr;
        }
        $rows = $this->couple->getList($userIds);
        $cpRows = array_column($rows, null, 'userId');

        $userData = $this->user->getUserInfoArrByIds(array_merge($userIds, $friendIds));
        $userDataArr = array_column($userData ,null ,'user_id');
        // 信物
        $keepsakes = $this->keepsake->getAllKeepsake();
        $keepsakesArr = array_column($keepsakes, null, 'id');
        $list = [];
        foreach ($rsCodes as $v) {
            $item = array();
            if (isset($cpRows[$v[2]]) && isset($userDataArr[$v[2]])) {
                $item = $cpRows[$v[2]];
                $item['userNickname'] = $userDataArr[$v[2]]['nickname'];
                $item['friendNickname'] = $userDataArr[$v[3]]['nickname'];
                $item['userUuid'] = $userDataArr[$v[2]]['uuid'];
                $item['friendUuid'] = $userDataArr[$v[3]]['uuid'];
                $item['userCpStatus'] = $userDataArr[$v[2]]['cp_status'];
                $item['friendCpStatus'] = $userDataArr[$v[3]]['cp_status'];
                if (isset($keepsakesArr[$item['keepsakeId']])) {
                    $item['keepsakeName'] = $keepsakesArr[$item['keepsakeId']]['keepsakeName'];
                } else {
                    $item['keepsakeName'] = $item['keepsakeId'];
                }
                $item['rsCode'] = $v[4];
                $list[] = $item;
            }
        }
        $return['list'] = $list;
        $return['total'] = $total;
        $return['curPage'] = $page;
        return $return;
    }

    public function getKeepsakeList ($page = 1, $pageSize = 10) {
        $return = array();
        $rows = $this->keepsake->getKeepsakeLsit(($page - 1) * $pageSize, $pageSize);
        $total = $this->keepsake->getKeepsakeCount();
        $propIds = array_column($rows, 'propId');
        $propsRows = $this->prop->getPropInfoByIds($propIds);
        $props = array_column($propsRows, null, 'prop_id');
        foreach ($rows as &$row) {
            if (isset($props[$row['propId']])) {
                $row['propName'] = $props[$row['propId']]['name'];
            } else {
                $row['propName'] = '';
            }
        }
        $return['list'] = $rows;
        $return['total'] = $total;
        $return['curPage'] = $page;
        return $return;
    }

    public function getRingList ($page, $pageSize, $type=-1, $ringName='',$isShow = '') {
        $offset = ($page - 1) * $pageSize;
        $rows = $pageSize;
        $data = $this->couplingRing->getRingList($offset, $rows, $type, $ringName,$isShow);
        $count = $this->couplingRing->getRingListCount($type, $ringName,$isShow);
        $return['list'] = $data;
        $return['total'] = $count;
        $return['curPage'] = $page;
        return $return;
    }

    public function getIntimateList ($rsCode, $type) {
        $tasks = $this->rsRelationshipTasks->getTasks(3);
        $reasons = array_column($tasks, null, 'id');
        $list = $this->relationshipIntimacyLog->getLogs($rsCode, $type);

        foreach ($list as &$v) {
            if ($v['taskId'] == 10001) {
                $v['reason'] = "信物赠送";
                continue;
            }
            if ($reasons[$v['taskId']]) {
                $v['reason'] = $reasons[$v['taskId']]['content'];
            } else {
                $v['reason'] = '';
            }
        }
        return $list;
    }

    /**
     * 获取获取某种用户关系的等级
     */
    public function getLevelList($type=0){
        $list = $this->rsRelationshipTasks -> getLevelList($type);

        return $list;
    }

    public function createKeepsake ($data) {
        return curl_request(getKkaudioApiUrl() . '/inner/couple/innerCreateKeepsake', $data, 'json', 'POST');
    }

    public function updateKeepsake ($data) {
        return curl_request(getKkaudioApiUrl() . '/inner/couple/innerUpdateKeepsake', $data, 'json', 'POST');
    }

    public function deleteKeepsake ($id) {
        $data = array('id'=>$id);
        return curl_request(getKkaudioApiUrl() . '/inner/couple/innerDeleteKeepsake', $data, 'json', 'POST');
    }

    public function createRing ($data) {
        return curl_request(getKkaudioApiUrl() . '/inner/couple/innerCreateRing', $data, 'json', 'POST');
    }

    public function updateRing ($data) {
        return curl_request(getKkaudioApiUrl() . '/inner/couple/innerUpdateRing', $data, 'json', 'POST');
    }
}
